The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java
NickName:prosseek Ask DateTime:2013-02-17T12:12:54

The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java

Let's say we have class A as a parent class, and class C that extends it.

class A
{
    void m()
    {
        System.out.println("A.m");
    }
}

class C extends A
{
    @Override
    void m()
    {
        System.out.println("C.m");
    }
}

What's the difference between reference A a and C c when we use them to point to the same object type, for example A a = new C(); and C c = new C();?

From this question: Java inheritance vs. C# inheritance, it looks like that as a and c points to object type of C, and there seems no difference in using them.

I tested this code, and they all prints C.m.

class inherit {

    void x(A a)
    {
        a.m();
    }

    public static void main(String[] args) {
        System.out.println("hello");
        A a = new C();
        C c = new C();
        a.m();
        c.m();

        new inherit().x(a);
        new inherit().x(c);

    }
}

Copyright Notice:Content Author:「prosseek」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14917903/the-difference-between-c-c-new-c-and-a-c-new-c-when-c-is-a-subclass

Answers
Hui Zheng 2013-02-17T04:34:08

That depends what the object is going to be used for. \n\nIf what you actually need is an object that has A's interface(i.e. A's type), it's strongly recommended to use A a = new C();. This way it makes it clear that you want an A interface, not a C implementation. Later when you change your mind, you can safely change it to A a = new Another_Subtype_Of_A(); without breaking other code.\n\nThis is especially true when A is an interface(In your case, A is a class). For example, if you just want a list, List list = new ArrayList(); is clearly better than ArrayList list = new ArrayList();. That's called \"programming to interface, not implementation\".\n\nIf you're creating an object that specifically needs C's interfaces(esp. those not present in A), you'd better choose C c = new C();. If you write A a = new C() instead, sooner or later you still have to cast the object to C(because A doesn't have all of your desired interfaces), so why bother?",


Dennis 2013-02-17T04:17:57

It's not about the runtime type of the variable. You may only know you have a Vehicle object at compile time and based on user input, that may be a GarbageTruck or SportsCar.\n\nGarbageTruck t;\n...\nt = new SportsCar(); //can't do this!\n\n// so we do this:\nVehicle t;\nif(user.isFast()) {\n t = new SportsCar();\n} else {\n t = new GarbageTruck();\n}\n",


Dancrumb 2013-02-17T04:23:37

Java is all about Interfaces and Implementations.\n\nAn Interface is simply a set of public fields (methods & properties) the describe how users can interact with a class that implements the interface.\n\nAn Implementation is the code that actually makes those methods and properties do something. An Implementation can be a class that implements an interface, or it could be a subclass of some other implementation.\n\nWhen you instantiate a class, you're writing code like:\n\nInterface a = new Implementation();\n\n\nOften times, we wrap the Interface and the Implementation all together... put another way, when we define a class, whether we're explicitly implementing an interface or not, we're defining an Interface with every public method we write.\n\nThus, it's the Interface that affects what methods we can call, but it's the Implementation that affects what happens when we call them.",


More about “The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java” related questions

The difference between

What is the difference between: request.post.get('blog','') AND request.post.get('blog') I am not able to figure out what is the difference between these two and what they return.

Show Detail

Difference between ./ and ../

Might be a fairly simple question to ask, however, I would love to know the difference between the two. I see them being used a lot when doing relative file linking for web design however I don't k...

Show Detail

Difference Between Datasets

Here is the problem statement: I have 2 datasets from different years(2013 dataset and 2014 dataset), the data is multivariate with each dataset containing 38 attributes, I want to find out any

Show Detail

Difference between Scriptable and programmable

I was confused as to what is the difference between a Script and a Program, but a previously asked question Difference between a script and a program? clarified my doubt but that further leads me to

Show Detail

Difference between Traits?

Is there any difference between these two trait's? scala> trait Bar[A <: Foo] { | def bippy(x: A): A | } defined trait Bar and scala> trait BarTwo { | type A <

Show Detail

Difference between $@ and $! in perl

What is the difference between $@ and $! in Perl? Errors associated with eval are outputted using $@ . $! is also used for capturing the error. Then what is the difference between both of them?

Show Detail

C difference between *[] and **

This might be a bit of a basic question, but what is the difference between writing char * [] and char **? For example, in main,I can have a char * argv[]. Alternatively I can use char ** argv. I a...

Show Detail

Difference between function and generator?

I reviewed the question https://stackoverflow.com/questions/29864146/difference-between-function-and-generator but it is marked as duplicate. So I am posting my version of the answer here. The ori...

Show Detail

Difference between function and generator?

I reviewed the question https://stackoverflow.com/questions/29864146/difference-between-function-and-generator but it is marked as duplicate. So I am posting my version of the answer here. The ori...

Show Detail

Difference between {!! !!} and {{}}

I am curious between the difference between {!! !!} and {{ }} In laravel, Model-View-Controller it is used in View where the user declare the variables which was sent from the Controller Example ...

Show Detail